<template>
    <div>
        <Form :label-width="100" inline>
            <Form-item label="行政区划">
                <dictSelect :sel_val='queryParam.cfab0001' dicType="service"
                            dictService="/api/ac/subsistence/extSysDictService/getSs01001List"
                            :bindData.sync="queryParam.cfab0001" style="width:200px"/>
            </Form-item>
            <Form-item label="救助类别">
                <dictSelect :sel_val='queryParam.cfab0002' dictCode="assistancetype"
                            :bindData.sync="queryParam.cfab0002" style="width: 200px" showKeys="100,200"/>
            </Form-item>
            <Form-item label="标准类别">
                <dictSelect :sel_val='queryParam.cfab0007' dictCode="standardtype" :bindData.sync="queryParam.cfab0007"
                            style="width: 200px"/>
            </Form-item>
            <Row class="button_row">
                <span style="float: left">
                     <Button type="primary" @click="handleAdd()">录入 </Button>
                </span>
                <span style="float: right">
                    <Button type="primary" @click="searchQuery">查询</Button>
                 <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
                </span>
            </Row>
        </Form>

        <Modal v-model="visible" :title="title" width="600" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules">
                <FormItem label="行政区划" prop="cfab0001">
                    <dictSelect :sel_val='formData.cfab0001' dicType="service" :bindData.sync="formData.cfab0001"
                                dictService="/api/ac/subsistence/extSysDictService/getSs01001List"
                                style="width: 300px"/>
                </FormItem>
                <FormItem label="救助类别" prop="cfab0002">
                    <dictSelect :sel_val='formData.cfab0002' dictCode="assistancetype"
                                :bindData.sync="formData.cfab0002" style="width: 300px" showKeys="100,200"/>
                </FormItem>
                <FormItem label="低保标准" prop="cfab0003">
                    <Input v-model="formData.cfab0003" placeholder="" number style="width: 300px"/>
                </FormItem>
                <FormItem label="低保边缘标准" prop="cfab0004">
                    <Input v-model="formData.cfab0004" placeholder="" number style="width: 300px"/>
                </FormItem>
                <FormItem label="生效时间" prop="cfab0005">
                    <DatePicker type="date" v-model="formData.cfab0005" placeholder="请选择标准生效时间"
                                style="width: 300px"  :editable="false"></DatePicker>
                </FormItem>
                <FormItem label="失效时间" prop="cfab0006">
                    <DatePicker type="date" v-model="formData.cfab0006" placeholder="请选择标准失效时间"
                                style="width: 300px"  :editable="false"></DatePicker>
                </FormItem>
                <FormItem label="标准文件名称" prop="cfab0009">
                    <Input v-model="formData.cfab0009" placeholder="" style="width: 300px" type="textarea" :rows="3"/>
                </FormItem>
                <FormItem label="标准文件号" prop="cfab0010">
                    <Input v-model="formData.cfab0010" placeholder="" style="width: 300px"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button @click="visible = false">关闭</Button>
                <Button type="primary" @click="handleSubmit('formData')" v-if="showSubmit">确定</Button>
            </div>
        </Modal>
        <div class="table_div">
            <Table :columns="columns" :data="tableData" :loading="Loading">
                <template slot-scope="{ row, index  }" slot="action">
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleView(index)">详情</Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'
    import DateFormat from '@/plugins/format.js'

    export default {
        mixins: [CommonMixin],
        components: {
            dictSelect
        },
        data() {
            return {
                rules: {
                    cfab0001: [
                        {required: true, message: '信息必填', trigger: 'blur'}
                    ],
                    cfab0002: [
                        {required: true, message: '信息必填', trigger: 'blur'}
                    ],
                    cfab0003: [
                        {required: true, message: '必须是数字类型', trigger: 'blur', type: 'number'}
                    ],
                    cfab0004: [
                        {required: true, message: '必须是数字类型', trigger: 'blur', type: 'number'}
                    ],
                    cfab0005: [
                        {required: true, message: '信息必填', trigger: 'blur', type: 'date'},
                    ],
                    cfab0006: [
                        {required: true, message: '信息必填', trigger: 'blur', type: 'date'},
                    ]
                },
                columns: [
                    {
                        title: '行政区划',
                        key: 'cfab0001_name'
                    },
                    {
                        title: '救助类别',
                        key: 'cfab0002_name'
                    },
                    {
                        title: '低保标准',
                        key: 'cfab0003'
                    },
                    {
                        title: '低保边缘标准',
                        key: 'cfab0004'
                    },
                    {
                        title: '生效时间',
                        key: 'cfab0005',
                        render: (h, params) => {
                            let value = params.row.cfab0005
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        }
                    },
                    {
                        title: '失效时间',
                        key: 'cfab0006',
                        render: (h, params) => {
                            let value = params.row.cfab0006
                            if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                                value = '--'
                            } else {
                                value = DateFormat(value, 'YYYY-MM-DD')
                            }
                            return h('span', value + ' ')
                        }
                    },
                    {
                        title: '标准类别',
                        key: 'cfab0007_name'
                    },
                    {
                        title: '标准文件号',
                        key: 'cfab0010'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        slot: 'action',
                        width: 150
                    }
                ],
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss06001Service/getPageList",
                    edit: "/api/ac/subsistence/ss06001Service/updateData",
                    add: "/api/ac/subsistence/ss06001Service/insertData"
                },
                formData: {
                    cfab0001: '',
                    cfab0002: '',
                    cfab0003: '',
                    cfab0004: '',
                    cfab0005: '',
                    cfab0006: '',
                    cfab0009: '',
                    cfab0010: ''
                },
                queryParam: {},
                visible: false,
                showSubmit: true,
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: ""
            }
        },
        methods: {
            handleAdd() {
                let that = this;
                that.title = "保障标准录入"
                let record = {};
                that.visible = true
                that.showSubmit = true
                that.formData = Object.assign({}, record)
            },
            handleEdit(index) {
                let that = this;
                that.title = "保障标准编辑"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.showSubmit = true
                    that.formData = Object.assign({}, record)
                    that.formData.cfab0005 = new Date(record.cfab0005);
                    that.formData.cfab0006 = new Date(record.cfab0006);
                } else {
                    that.visible = false
                }
            },
            handleView(index) {
                let that = this;
                that.title = "保障标准查看"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.showSubmit = false
                    that.formData = Object.assign({}, record)
                    that.formData.cfab0005 = new Date(record.cfab0005);
                    that.formData.cfab0006 = new Date(record.cfab0006);
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let that = this;
                        let dataParam = {
                            id: that.formData.id,
                            cfab0001: that.formData.cfab0001,
                            cfab0002: that.formData.cfab0002,
                            cfab0003: that.formData.cfab0003,
                            cfab0004: that.formData.cfab0004,
                            cfab0005: DateFormat(that.formData.cfab0005, 'YYYY-MM-DD'),
                            cfab0006: DateFormat(that.formData.cfab0006, 'YYYY-MM-DD'),
                            cfab0009: that.formData.cfab0009,
                            cfab0010: that.formData.cfab0010
                        }
                        let url = null;
                        if (dataParam.id == null) {
                            url = that.url.add
                        } else {
                            url = that.url.edit
                        }
                        postAction(url, dataParam).then((res) => {
                            if (res.data.data.code == 1) {
                                that.visible = false
                                this.$Message.success("提交成功！")
                                this.loadData();
                            } else {
                                this.$Message.error(res.data.data.msg);
                            }
                        }).finally(() => {

                        })
                    } else {
                        this.$Notice.error({title: '必填信息未填写完成!', desc: '必填信息未填写完成!'})
                    }
                });
            }
        },
        created() {
            this.loadData();
        },
    }
</script>
<style scoped>
    .paging {
        float: right;
        margin-top: 10px;
    }

    /deep/ .ivu-upload-list {
        display: none;
    }
</style>
